<template>
  <div class="addWallet">
    <x-header :left-options="{backText: ''}">{{id?'修改钱包地址':'添加钱包地址'}}</x-header>
    <div class="bankSty">
      <div class="bankNumber">
        <span>钱包名称:</span>
        <input type="" v-model="walletName">
      </div>
      <div class="bankNumber">
        <span>钱包地址:</span>
        <input type="" v-model="walletAddress">
      </div>
      <div class="bankNumber">
        <span>手机验证:</span>
        <input type="text" style="margin-right: 70px;width: 50%;" v-model="phoneNumber">
        <button class="sendNumber" @click="getNumber" :disabled='isDisabled'>{{verificationCode}}</button>
      </div>
      <div class="confimBtn" @click="saveAddress" v-if="!id">
        <span>保存地址</span>
      </div>
      <div class="confimBtn" @click="editAddress" v-if="id">
        <span>保存地址</span>
      </div>
    </div>
  </div>
</template>
<script>
  import { XHeader } from 'vux'
  import tips from '../../service/tips'
  import http from '../../service/http'
  export default {
    data() {
      return {
        verificationCode: "发送验证码",
        countDown: 60,
        isDisabled: false,
        phoneNumber: "",
        walletName: "",
        walletAddress: "",
        id: "",
      }
    },
    mounted() {
      this.id = location.hash.split("=")[1];
    },
    methods: {
      getNumber() {
        this.countDown = 60;
        this.verificationCode = this.countDown + "s";
        var timer = setInterval(() => {
          this.countDown--;
          this.isDisabled = true;
          if (this.countDown <= 1) {
            this.isDisabled = false;
            this.countDown = '再次获取';
            clearInterval(timer);
          }
          if (this.countDown >= 0) {
            this.verificationCode = this.countDown + "s";
          } else {
            this.verificationCode = this.countDown
          }
        }, 1000)
        var phoneNum = window.localStorage.getItem('login.username');
        http.post("/user/sms", {
          username: phoneNum
        }).then((res) => {
          if (res.data.status == 200) {
            tips.tips("手机验证发送成功");
          }
        }).catch((res) => {
          tips.tips(res.data.msg)
        })
      },
      saveAddress() {
        if (!this.walletName) {
          tips.tips("请输入钱包名称")
          return false;
        }
        if (!this.walletAddress) {
          tips.tips("请输入钱包地址")
          return false;
        }
        if (!this.phoneNumber) {
          tips.tips("请输入手机验证");
          return false;
        }
        http.put("/user/address", {
          type: this.walletName,
          address: this.walletAddress,
          smscode: this.phoneNumber
        }).then((res) => {
          if (res.data.status == 200) {
            tips.tips(res.data.msg);
            history.back();
          } else {
            tips.tips(res.data.msg);
          }
        }).catch((res) => {
          tips.tips(res.data.msg);
        })
      },
      editAddress() {
        http.post("user/address", {
          id: this.id,
          type: this.walletName,
          address: this.walletAddress,
          smscode: this.phoneNumber
        }).then((res) => {
          if (res.data.status == 200) {

          }else{
            tips.tips(res.data.msg)
          }
        }).catch((res)=>{
          tips.tips(res.data.msg)
        })

      }
    },
    components: {
      XHeader,
    }
  }
</script>
<style lang="less">

  .addWallet .vux-header {
    background: #fff;
  }

  .addWallet .vux-header .vux-header-title {
    line-height: 40px;
    text-align: center;
    font-size: 18px;
    font-weight: 400;
    color: #000;
  }

  .addWallet .vux-header .vux-header-left .left-arrow:before {
    content: "";
    position: absolute;
    width: 12px;
    height: 12px;
    border: 1px solid #000;
    border-width: 1px 0 0 1px;
    -webkit-transform: rotate(315deg);
    transform: rotate(315deg);
    top: 8px;
    left: 7px;
  }

  .addWallet {
    background: #fff;
  }

  .addWallet .bankSty {
    margin-top: 10px;
    position: relative;
    min-height: 350px;
    border-top: 1px solid #f6f6f6;
  }

  .addWallet .bankSty input {
    border: none;
    border-style: none;
    outline: none;
    position: absolute;
    right: 15px;
    text-align: right;
    display: inline-block;
    width: 70%;
    height: 40px;
    line-height: 40px;
  }

  .addWallet .bankNumber {
    height: 40px;
    line-height: 40px;
    border-bottom: 1px solid #f6f6f6;
    padding-left: 15px;
    span {
      font-size: 16px;
    }
    .sendNumber {
      position: absolute;
      right: 15px;
      color: #fea620;
      font-size: 12px;
      border: none;
      background: #fff;
      outline: none;
      margin-top: 10px;
      width: 60px;
      text-align: center;
    }
  }

  .addWallet .confimBtn {
    margin-top: 100px;
    padding: 0px 15px;
    text-align: center;
    span {
      display: block;
      height: 40px;
      line-height: 40px;
      background: linear-gradient(left, #fedb30, #fe9f0a);
      background-image: linear-gradient(-135deg, #fedb30 0%, #fe9f0a 100%);
      width: 100%;
      text-align: center;
      color: #fff;
      border-radius: 25px;
    }
    p {
      color: red;
      font-size: 13px;
      margin-top: 20px;
    }
  }
</style>